package com.my.myTradeData;

import java.util.List;

import com.my.stock.core.KLine;

public class TechnicalAnalysisIndicators {

	public static KLine getMaxDayData(List<KLine> dayDataList, int paraDays) {
		int icount;
		if (dayDataList.size() < paraDays) {
			icount = dayDataList.size();
		} else {
			icount = paraDays;
		}

		double max = 0;
		int maxi = 0;
		for (int i = 0; i < icount; i++) {

			if (dayDataList.get(i).getHigh() > max) {
				max = dayDataList.get(i).getHigh();
				maxi = i;
			}

		}
		return dayDataList.get(maxi);

	}

	public static KLine getMinDayData(List<KLine> dayDataList, int paraDays) {
		int icount;
		if (dayDataList.size() < paraDays) {
			icount = dayDataList.size();
		} else {
			icount = paraDays;
		}

		double min = Integer.MAX_VALUE;
		int mini = 0;
		for (int i = 0; i < icount; i++) {

			if (dayDataList.get(i).getLow() != 0 && dayDataList.get(i).getLow() < min) {
				min = dayDataList.get(i).getLow();
				mini = i;
			}

		}
		return dayDataList.get(mini);

	}

	public static double getTr(KLine kline, KLine preKLine) {
		double atr1 = Math.abs(kline.getHigh() - kline.getLow());
		double atr2 = Math.abs(preKLine.getClose() - kline.getLow());
		double atr3 = Math.abs(preKLine.getClose() - kline.getHigh());
		double atr = Math.max(atr1, atr2);
		atr = Math.max(atr, atr3);
		return atr;
	}

	public static double getTr(KLine kline) {
		double atr1 = Math.abs(kline.getHigh() - kline.getLow());
		double atr2 = Math.abs(kline.getOpen() - kline.getLow());
		double atr3 = Math.abs(kline.getOpen() - kline.getHigh());
		double atr = Math.max(atr1, atr2);
		atr = Math.max(atr, atr3);
		return atr;
	}

	public static double getAtr(List<KLine> dataList) {
		double cout = 0;
		for (int i = 0; i < dataList.size(); i++) {
			cout = cout + dataList.get(i).getTr();
		}

		return cout / dataList.size();

	}

	public static double getAtr1(List<KLine> dataList, int paraDays) {
		int icount;
		if (dataList.size() < paraDays) {
			icount = dataList.size();
		} else {
			icount = paraDays;
		}
		double cout = 0;
		for (int i = 0; i < icount - 1; i++) {
			double atr1 = Math.abs(dataList.get(i).getHigh() - dataList.get(i).getLow());
			double atr2 = Math.abs(dataList.get(i + 1).getClose() - dataList.get(i).getLow());
			double atr3 = Math.abs(dataList.get(i + 1).getClose() - dataList.get(i).getHigh());
			double atr = Math.max(atr1, atr2);
			atr = Math.max(atr, atr3);
			cout = cout + atr;
		}

		return cout / paraDays;

	}

	public static double getAtr(KLine[] dataList, int paraDays) {
		double cout = 0;
		for (int i = 0; i < paraDays; i++) {
			double atr1 = Math.abs(dataList[i].getHigh() - dataList[i].getLow());
			double atr2 = Math.abs(dataList[i + 1].getClose() - dataList[i + 1].getLow());
			double atr3 = Math.abs(dataList[i + 1].getClose() - dataList[i + 1].getHigh());
			double atr = Math.max(atr1, atr2);
			atr = Math.max(atr, atr3);
			cout = cout + atr;
		}

		return cout / paraDays;

	}

	public static boolean majorTrend(List<DayData> dataList) {
		int max = dataList.size() - 1;
		if (dataList.size() >= 201) {
			max = 200;
		}

		double value = dataList.get(0).getClose() - dataList.get(max).getClose();
		if (value > 0) {
			return true;
		} else {
			return false;
		}
	}

}
